package com.shareit.ocr.domain.simulator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.shareit.ocr.domain.Dama2Web;
import com.shareit.ocr.domain.Dama2Web.DecodeResult;
import com.shareit.ocr.domain.SSLClient;
import com.shareit.ocr.domain.template.InvoiceDataTemplate;
import com.shareit.ocr.enums.ResultEnum;
import com.shareit.ocr.exception.SimulatorException;
import com.shareit.ocr.util.InvoiceQueryUtil;
import com.shareit.ocr.util.PicUtil;

public class APISimulator extends BaseSimulator {
	
	private final Logger logger = LoggerFactory.getLogger(APISimulator.class);
	
	private String yzmSj ="";
	private String index ="";
	private String yzm = "";
	private String yzmtype= "00";
	private String detail = "";
	public APISimulator() {
		super();	
	}
	@Override
	public void close() {
		
	}

	@Override
	public Logger getLogger() {
	 
		return this.logger;
	}

	@Override
	public void getYzmCode(InvoiceDataTemplate idt) throws Exception {
	 
		String yzmURL = InvoiceQueryUtil.getYzmUrl(idt.getHeader().getFieldValue("fpdm"));
		Map<String,String> map = new HashMap<String,String>();
		map.put("fpdm",idt.getHeader().getFieldValue("fpdm"));
		map.put("r", "0.788234832882");
		map.put("nowtime",String.valueOf(System.currentTimeMillis()));
		map.put("publickey", "1234567890");
		map.put("_","1515996407150");
		map.put("callback","jQuery0");
		yzmtype = "";
		String yzmCode ="";
		String yzminfo = "";
		do {
			infoLog("请求验证码:" + yzmURL);
			String reString = SSLClient.doGet(map,yzmURL, "utf8", "utf8");
			reString =reString.substring(8,reString.length() -1);
			JSONObject jsonObject = JSONObject.parseObject(reString);
			yzmSj = jsonObject.getString("key2");
			index = jsonObject.getString("key3");
			yzmCode = jsonObject.getString("key1");
			yzmtype =jsonObject.getString("key4");
			infoLog("yzmtype:" + yzmtype);
		}while("00".equals(yzmtype));
		if(yzmtype.equalsIgnoreCase("01")) {yzminfo="红色验证码";}
		else if (yzmtype.equalsIgnoreCase("02")){yzminfo="黄色验证码";}
		else if (yzmtype.equalsIgnoreCase("03")){yzminfo="蓝色验证码";}
		
		DecodeResult decoderesult = null;
		Dama2Web dw = new Dama2Web(this.simulatorProperties.getDamaProperties());
		infoLog("处理验证码图片");
		byte[] data = PicUtil.getCodeData(yzmCode, yzminfo, 30);
		infoLog("处理验证码图片完成，开始获取验证码结果");
//		decoderesult = dw.d2File(200, 60, data);
		decoderesult = dw.d2File(31, 60, data);
		yzm =  decoderesult.result;
		infoLog("获取到验证码结果 " + yzm);
		
	}
	@Override
	public void getDetailInfo(InvoiceDataTemplate idt) throws Exception {
 
		String fpdm = idt.getHeader().getFieldValue("fpdm");
		String fphm = idt.getHeader().getFieldValue("fphm");
		String fpje = idt.getHeader().getExtInfo();
		String fplx = InvoiceQueryUtil.getInvoiceType(fpdm);
		String kprq = idt.getHeader().getFieldValue("kprq");
		
		String url = InvoiceQueryUtil.getDetailUrl(fpdm);
		Map<String,String> map = new HashMap<String,String>();
		
		
		map.put("fpdm", fpdm);
		map.put("fphm", fphm);
		map.put("fpje", fpje);
		map.put("fplx", fplx);
		map.put("yzm",yzm);
		map.put("yzmSj",yzmSj);
		map.put("index",index);
		map.put("iv","f2ba81f88ec903c1c89faa4bde23d9ba");
		map.put("kprq", kprq);
		map.put("salt","c0d451bbed678c26f76c23b1b475b5aa");
		map.put("publickey", "9BD60F9600FAF6F2249A77AA39DAC308");
		map.put("callback","jQuery1");
		
		detail =SSLClient.doGet(map,url, "utf-8", "utf-8");
		detail = detail.substring(8,detail.length() -1);
		infoLog(detail);
		JSONObject jsonObject = JSONObject.parseObject(detail);
		String resultcode = jsonObject.getString("key1");
		infoLog("验证码结果:" + resultcode);
		ResultEnum re = InvoiceQueryUtil.getResultWithCode(resultcode);
		if(re != ResultEnum.SUCCESS) {
			throw new SimulatorException(re);
		}
		
		idt.getHeader().setDataFields(detail);
		
		List<String> rows = Arrays.asList(jsonObject.get("key3").toString().split(idt.getItems().getSplitChar()));
		idt.getItems().setItemsData(rows);	
	}

}
